package com.kugua.middleware.rocketmq;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.MessageQueueSelector;
import com.alibaba.rocketmq.client.producer.SendCallback;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;
import com.alibaba.rocketmq.remoting.RPCHook;
import com.alibaba.rocketmq.remoting.protocol.RemotingCommand;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>TODO</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年11月28日 10:28
 * @since 1.0
 */
public class Producer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("zf_dev_test_mq_1");
//        producer.setNamesrvAddr("10.16.37.110:9876");
        producer.setNamesrvAddr("10.16.37.109:9876;10.16.37.110:9876");
//        producer.setCreateTopicKey("CuiyhTopic4");
        producer.start();
        for (int i = 0; i < 30; i++) {
            try {
                int orderId = i % 10;
                Message msg = new Message("CuiyhTopic4", "TagA", ("Hello RocketMQ sendOneway:" + i).getBytes());
                SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                        Integer id = (Integer)o;
                        int index = id % list.size();
                        return list.get(index);
                    }
                }, orderId);
                System.out.println("sendResult:" + sendResult);
//                SendResult sendResult = producer.send(msg);
//                producer.send(msg, new SendCallback() {
//                    @Override
//                    public void onSuccess(SendResult sendResult) {
//                        System.out.println("onSuccess:" + sendResult);
//                    }
//
//                    @Override
//                    public void onException(Throwable throwable) {
//                        System.out.println("onException");
//                    }
//                });


            } catch (Exception e) {
                e.printStackTrace();
            }
            TimeUnit.SECONDS.sleep(2);
        }
        producer.shutdown();
    }
}
